package com.java;

/**
 * 中断线程
 */
public class InterruptThread {
    public static void main(String[] args) {
        System.out.println("主线程开始");
        
        Thread thread = Thread.currentThread();
        System.out.println(thread.isInterrupted()); // false
        
        // 被中断后不能sleep
        thread.interrupt();
        System.out.println(thread.isInterrupted()); // true
        System.out.println(thread.isInterrupted()); // true
        
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) { // 被捕获后自动结束中断
            e.printStackTrace(); // java.lang.InterruptedException: sleep interrupted
        }
        System.out.println(thread.isInterrupted()); // false
        
        thread.interrupt();
        System.out.println(thread.isInterrupted()); // true
        
        System.out.println(Thread.interrupted()); // true
        System.out.println(Thread.interrupted()); // false
        System.out.println(Thread.interrupted()); // false
        
        System.out.println("主线程结束");
    }
}
